package com;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test3 {
    //通过类名、方法名、方法参数，动态调用类中的某个方法
    public static Object execute(String className, String methodName, Object args[]) {
        try {
            Class c=Class.forName(className);
            Class[] c2 = new Class[args.length];

            for (int i = 0; i < args.length; i++) {

                c2[i] = args[i].getClass();
            }
                Method method = c.getMethod(methodName, c2);

                Object obj = c.newInstance();

            Object invoke = method.invoke(obj, args);
            return invoke;

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        execute("com.user","getName",args);
    }
}
